#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
using T = int;
T rad(); // quick read
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
const int max_size = 5 + 100;
const int maxn = 5 + 100;

struct Node {
    ll x, y;
    bool operator<(const Node &r) const { return x != r.x ? x < r.x : y < r.y; }
    Node operator*(ll r) const { return Node{x * r, y * r}; }
    Node operator-(const Node &r) const { return Node{x - r.x, y - r.y}; }
};

// 使用线性组合消除 a.x
void zhanzhuan(Node &a, Node &b) {
    while (a.x != 0) {
        // printf("%d %d %d %d\n", a.x, a.y, b.x, b.y);
        b = b - a * (b.x / a.x);
        swap(a, b);
    }
}

vector<Node> a;

inline void update() {
    if (a.size() <= 1) return;
    if (a.size() == 2) {
        zhanzhuan(a[0], a[1]);
        a[1].y %= a[0].y;
        return;
    }
    zhanzhuan(a[2], a[1]);
    a[0].y = __gcd(a[0].y, a[2].y);
    a[1].y %= a[0].y;
    a.pop_back();
}

inline bool check(int x, int y) {
    if (a.size() == 0) return 0;
    if (a.size() == 1) return x * a[0].y == y * a[0].x;
    return x % a[1].x == 0 &&(y - x / a[1].x * a[1].y) % a[0].y == 0;
}

int main() {
    int Q = rad();
    rf(q, Q) {
        int n = rad();
        ll cnt = 0;
        a.clear();
        rf(i, n) {
            int op = rad();
            if (op == 1) {
                int x = rad(), y = rad();
                a.push_back({x, y});
                update();
                // printf("now: ");
                // for (auto p : a)
                //     printf("%d %d ", p.x, p.y);
                // puts("");
            } else if (op == 2) {
                int x = rad(), y = rad(), w = rad();
                if (check(x, y)) cnt += w;
            } else if (op == 3) {
            }
        }
        printf("Case #%d: %lld\n", q, cnt);
    }
}

T rad() {
    T back = 0;
    int ch = 0, posi = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        posi = ch ^ '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return posi ? back : -back;
}

/*
1000 1 2 2 3


1
100
1 2 3
1 4 0
1 3 3
1 0 4

2
4
1 1 1
2 3 1 1
1 1 3
2 3 1 2
3
1 1 1
1 2 1
2 3 2 3


1
3
1 1 1
1 2 1
2 3 2 3


*/
